package sg.edu.nus.iss.billing.ui.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Formvalidation {
	
	public boolean validateDate(String dateStr,boolean allowPast,String formatStr){
		
		if (formatStr == null) return false;
		SimpleDateFormat df = new SimpleDateFormat(formatStr);
		Date testDate = null;
		try{
			testDate = df.parse(dateStr);
		}
		catch (ParseException e){
		return false;
		}
		if (!allowPast){
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			if (cal.getTime().after(testDate)) return false;
		}

		if (!df.format(testDate).equals(dateStr)) return false;
		return true;
	}
	
	public boolean validateNumber(String number) {
		return number.matches("^\\d*$");
	} 
	
	/*public static String validateInteger(String input){
		String errorMessage;
		int number;
		try
		{
		    number = Integer.parseInt(input);
		    if(number < 0)
		    {
		    	errorMessage = "You positive number is";
		    }
		} catch (NumberFormatException ex)
		{
		     System.out.println("That is not a positive number!");
		}
	}*/
}
